package leetcode.problems;

import org.junit.Test;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by gmwang on 2018/7/25
 */
public class _0725UniqueMorseCodeWords {
    /**
     *
     International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on.
     国际摩尔斯电码定义了一种标准编码，其中每个字母映射到一系列点和短划线，如下所示："a"映射到".-"，"b"映射到"-..."，"c"映射到"-.-."等等。
     For convenience, the full table for the 26 letters of the English alphabet is given below:
     为方便起见，下面给出了英文字母26个字母的完整表格：
     [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
     Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, "cab" can be written as "-.-.-....-", (which is the concatenation "-.-." + "-..." + ".-"). We'll call such a concatenation, the transformation of a word.
     现在，给定一个单词列表，每个单词可以写成每个字母的摩尔斯代码的串联。例如，“cab”可以写成“-.-.-....-”，（这是串联“-.-.”+“ -...”+”.-“）。我们称之为串联，即一个词的转换。

     返回我们所有单词中不同变换的数量。
     Return the number of different transformations among all words we have.

     Example:
     Input: words = ["gin", "zen", "gig", "msg"]
     Output: 2
     Explanation:
     The transformation of each word is:
     "gin" -> "--...-."
     "zen" -> "--...-."
     "gig" -> "--...--."
     "msg" -> "--...--."

     There are 2 different transformations, "--...-." and "--...--.".


     Note:

     The length of words will be at most 100.
     Each words[i] will have length in range [1, 12].
     words[i] will only consist of lowercase letters.
     */
    public int uniqueMorseRepresentations(String[] words) {
        String [] strs = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        List<String> list = Arrays.asList(strs);
        Set<String> wordSet = new HashSet<>();
        StringBuilder sbChars;
        for(String word : words){
            sbChars = new StringBuilder();
            for(char w : word.toCharArray()){
                sbChars.append(list.get(w-97));
            }
            wordSet.add(sbChars.toString());
        }
        return wordSet.size();
    }
    @Test
    public void test(){
        String [] str = {"gin","zen","gig","msg"};
        int res = uniqueMorseRepresentations(str);
        System.out.println(res);
    }
}
